package com.example.tnot.Teacher.CoursePart.CourseInternal.CourseDetailHome.Statistics.ClockIn.Adapter;

import static com.example.tnot.Teacher.CoursePart.CourseInternal.CourseDetailHome.StudentList.Adapter.rv_StudentListAdapter.BASE_AVATAR_URL;

import android.annotation.SuppressLint;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.core.content.ContextCompat;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.resource.bitmap.CenterCrop;
import com.bumptech.glide.load.resource.bitmap.RoundedCorners;
import com.bumptech.glide.request.RequestOptions;
import com.example.tnot.R;
import com.example.tnot.Teacher.CoursePart.CourseInternal.CourseDetailHome.Statistics.ClockIn.ItemBean.ClockIn;
import com.example.tnot.Teacher.CoursePart.CourseInternal.CourseDetailHome.Statistics.ClockIn.ItemBean.InternalClockIn;

import org.json.JSONArray;
import org.json.JSONException;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

public class rv_ClockInAdapter extends RecyclerView.Adapter<rv_ClockInAdapter.ViewHolder> {
    private List<ClockIn> data;
    private List<InternalClockIn> internal_data;
    private JSONArray date_list;

    private rv_ClockInInternalAdapter rv_ClockInInternalAdapter;
    private ArrayList<String> status;
//    private boolean arrow_flag;


    public rv_ClockInAdapter(List<ClockIn> data) {
        this.data = data;
    }

    @NonNull
    @Override
    public rv_ClockInAdapter.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext())
                .inflate(R.layout.item_rv_lock_in, parent, false);
        return new ViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull rv_ClockInAdapter.ViewHolder holder, int position) {
        int NumOfPresent,NumOfLate,NumOfAbsence,NumOfSick,NumOfLeave;
        List NumOfClockInType;

        if (holder.isExpanded) {
            holder.ll_detail.setVisibility(View.VISIBLE);
            holder.iv_arrow.setImageResource(R.drawable.pull_down);
        } else {
            holder.ll_detail.setVisibility(View.GONE);
            holder.iv_arrow.setImageResource(R.drawable.pull_up);
        }

        //ll_detail默认消失
        holder.ll_detail.setVisibility(View.GONE);
        holder.iv_arrow.setImageResource(R.drawable.pull_up);

//        arrow_flag = true; //true表示箭头向上,false表示箭头向下,箭头向上表示收起,箭头向下表示展开
        holder.tv_name.setText(data.get(position).getName());
        date_list = data.get(position).getDate_list();
        String imageUrl = BASE_AVATAR_URL + data.get(position).getIcon();
        holder.times= (ArrayList<Integer>) data.get(position).getNumOfClockInType();
        RequestOptions options = new RequestOptions()
                .placeholder(R.drawable.example_face)
                .error(R.drawable.example_face)
                .transform(new CenterCrop(), new RoundedCorners(8));

//        Glide.get(holder.itemView.getContext()).clearMemory();  // 清除内存缓存（需在主线程）
//        new Thread(() -> {
//            // 清除磁盘缓存（需在子线程）
//            Glide.get(holder.itemView.getContext()).clearDiskCache();
//        }).start();

        Glide.with(holder.itemView.getContext())
                .load(imageUrl)
                .apply(options)
                .into(holder.iv_icon);

        //holder.iv_icon.setImageResource(data.get(position).getIcon());

        NumOfClockInType = data.get(position).getNumOfClockInType();
        int TotalClockIn = 0;
        for (int i = 0; i < NumOfClockInType.size(); i++){
//            累加获取总签到数有多少个
            TotalClockIn += (int) NumOfClockInType.get(i);
        }
        NumOfPresent = (int)NumOfClockInType.get(0);
        NumOfLate = (int)NumOfClockInType.get(1);
        NumOfAbsence = (int)NumOfClockInType.get(2);
        NumOfSick = (int)NumOfClockInType.get(3);
        NumOfLeave = (int)NumOfClockInType.get(4);

        //设置各个签到类型的数据
        holder.tv_NumOfPresent.setText(NumOfPresent+"");
        holder.tv_NumOfLate.setText(NumOfLate+"");
        holder.tv_NumOfAbsence.setText(NumOfAbsence+"");
        holder.tv_NumOfSick.setText(NumOfSick+"");
        holder.tv_NumOfLeave.setText(NumOfLeave+"");
        status = new ArrayList<>();

        status.add("出勤");
        status.add("迟到");
        status.add("缺勤");
        status.add("病假");
        status.add("事假");
        //内部列表初始仅展示 出勤情况
        try {
            InitInternal_data(holder.times,NumOfPresent);
        } catch (JSONException e) {
            throw new RuntimeException(e);
        }
        rv_ClockInInternalAdapter=new rv_ClockInInternalAdapter(internal_data);
        holder.rv_LockinInternal.setAdapter(rv_ClockInInternalAdapter);
        holder.rv_LockinInternal.setLayoutManager(new LinearLayoutManager(holder.itemView.getContext()));

        //设置每项item(数据框右侧)的签到数及总签到数
        holder.tv_all_clock_in.setText("/"+TotalClockIn + "");
        holder.tv_clock_in.setText(NumOfPresent+"");

        holder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                // 切换状态并更新UI
                holder.isExpanded = !holder.isExpanded;
                if (holder.isExpanded) {
                    holder.iv_arrow.setImageResource(R.drawable.pull_down);
                    holder.ll_detail.setVisibility(View.VISIBLE);
                } else {
                    holder.iv_arrow.setImageResource(R.drawable.pull_up);
                    holder.ll_detail.setVisibility(View.GONE);
                }
            }
        });


        holder.ll_attendance.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String DetailType = "出勤";
                //根据用户点击,展示对应的数据
                try {
                    SwitchInternalClockInData(holder.times.get(0),DetailType,0);
                } catch (JSONException e) {
                    throw new RuntimeException(e);
                }
                holder.rv_LockinInternal.setAdapter(new rv_ClockInInternalAdapter(internal_data));
                holder.rv_LockinInternal.setLayoutManager(new LinearLayoutManager(holder.itemView.getContext()));
            }
        });
        holder.ll_absence.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String DetailType = "缺勤";
                try {
                    SwitchInternalClockInData(holder.times.get(2),DetailType,2);
                } catch (JSONException e) {
                    throw new RuntimeException(e);
                }
                holder.rv_LockinInternal.setAdapter(new rv_ClockInInternalAdapter(internal_data));
                holder.rv_LockinInternal.setLayoutManager(new LinearLayoutManager(holder.itemView.getContext()));
            }
        });
        holder.ll_late.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String DetailType = "迟到";
                try {
                    SwitchInternalClockInData(holder.times.get(1),DetailType,1);
                } catch (JSONException e) {
                    throw new RuntimeException(e);
                }
                holder.rv_LockinInternal.setAdapter(new rv_ClockInInternalAdapter(internal_data));
                holder.rv_LockinInternal.setLayoutManager(new LinearLayoutManager(holder.itemView.getContext()));
            }
        });

        holder.ll_leave.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String DetailType = "事假";
                try {
                    SwitchInternalClockInData(holder.times.get(4),DetailType,4);
                } catch (JSONException e) {
                    throw new RuntimeException(e);
                }
                holder.rv_LockinInternal.setAdapter(new rv_ClockInInternalAdapter(internal_data));
                holder.rv_LockinInternal.setLayoutManager(new LinearLayoutManager(holder.itemView.getContext()));
            }
        });
        holder.ll_sick.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String DetailType = "病假";
                try {
                    SwitchInternalClockInData(holder.times.get(3),DetailType,3);
                } catch (JSONException e) {
                    throw new RuntimeException(e);
                }
                holder.rv_LockinInternal.setAdapter(new rv_ClockInInternalAdapter(internal_data));
                holder.rv_LockinInternal.setLayoutManager(new LinearLayoutManager(holder.itemView.getContext()));
            }
        });
    }
    private void SwitchInternalClockInData(int number,String type,int i) throws JSONException {

        Log.d("获取时间",date_list.getString(i));
        Log.d("时间获取次数", String.valueOf(number));
        internal_data.clear();
        for(int j=0;j<number;j++)
        {
            internal_data.add(new InternalClockIn(date_list.getJSONArray(i).getString(i),type));
        }

//        switch (type){
//            case "出勤":
//                for(int i = 0; i < number; i++){
//
//                    internal_data.add(new InternalClockIn(date_list.getJSONArray(0).getString(i),"出勤"));
//            }
//                break;
//            case "迟到":
//                for(int i = 0; i < number; i++){
//                    internal_data.add(new InternalClockIn(date_list.getJSONArray(1).getString(i),"迟到"));
//                }
//                break;
//            case "缺勤":
//                for(int i = 0; i < number; i++){
//                internal_data.add(new InternalClockIn(date_list.getJSONArray(2).getString(i),"缺勤"));
//            }
//                break;
//            case "病假":
//                for(int i = 0; i < number; i++){
//                    internal_data.add(new InternalClockIn(date_list.getJSONArray(3).getString(i),"病假"));
//                }
//                break;
//            case "事假":
//                for(int i = 0; i < number; i++){
//                internal_data.add(new InternalClockIn(date_list.getJSONArray(4).getString(i),"事假"));
//            }
//                break;
//
//            default:
//                //若出现错误就生成巨多数据
//                internal_data.add(new InternalClockIn("2023-07-12 12:40","出勤"));
//                internal_data.add(new InternalClockIn("2023-07-12 12:40","出勤"));
//                internal_data.add(new InternalClockIn("2023-07-12 12:40","出勤"));
//                internal_data.add(new InternalClockIn("2023-07-12 12:40","出勤"));
//                internal_data.add(new InternalClockIn("2023-07-12 12:40","出勤"));
//                internal_data.add(new InternalClockIn("2023-07-12 12:40","出勤"));
//                internal_data.add(new InternalClockIn("2023-07-12 12:40","出勤"));
//                internal_data.add(new InternalClockIn("2023-07-12 12:40","出勤"));
//                internal_data.add(new InternalClockIn("2023-07-12 12:40","出勤"));
//                internal_data.add(new InternalClockIn("2023-07-12 12:40","出勤"));
//                internal_data.add(new InternalClockIn("2023-07-12 12:40","出勤"));
//                internal_data.add(new InternalClockIn("2023-07-12 12:40","出勤"));
//                break;
//        }
       // rv_ClockInInternalAdapter.notifyDataSetChanged();
    }

    //初始化数据列表,在不点击下仅展示出勤数据
    private void InitInternal_data(ArrayList<Integer> times, int number) throws JSONException {
        internal_data = new ArrayList<>();
        SwitchInternalClockInData(times.get(0),"出勤",0);
    }

    @Override
    public int getItemCount() {
        return data.size();
    }

    public static class ViewHolder extends RecyclerView.ViewHolder{
        public boolean isExpanded;
        ImageView iv_icon,iv_arrow,iv_isToday;
        TextView tv_name;
        TextView tv_clock_in,tv_all_clock_in,tv_NumOfPresent,tv_NumOfAbsence,tv_NumOfLate,tv_NumOfLeave,tv_NumOfSick;
        LinearLayout ll_detail,ll_attendance,ll_absence,ll_late,ll_leave,ll_sick;

        ArrayList<Integer> times;

        RecyclerView rv_LockinInternal;
        public ViewHolder(@NonNull View itemView) {
            super(itemView);
            iv_icon = itemView.findViewById(R.id.iv_icon);
            iv_isToday = itemView.findViewById(R.id.iv_isToday);
            tv_name = itemView.findViewById(R.id.tv_name);
            tv_clock_in = itemView.findViewById(R.id.tv_clock_in);
            tv_all_clock_in = itemView.findViewById(R.id.tv_all_clock_in);
            tv_NumOfPresent = itemView.findViewById(R.id.tv_NumOfPresent);
            tv_NumOfAbsence = itemView.findViewById(R.id.tv_NumOfAbsence);
            tv_NumOfLate = itemView.findViewById(R.id.tv_NumOfLate);
            tv_NumOfLeave = itemView.findViewById(R.id.tv_NumOfLeave);
            tv_NumOfSick = itemView.findViewById(R.id.tv_NumOfSick);
            iv_arrow = itemView.findViewById(R.id.iv_arrow);
            ll_detail = itemView.findViewById(R.id.ll_detail);
            ll_attendance = itemView.findViewById(R.id.ll_attendance);
            ll_absence = itemView.findViewById(R.id.ll_absence);
            ll_late = itemView.findViewById(R.id.ll_late);
            ll_leave = itemView.findViewById(R.id.ll_leave);
            ll_sick = itemView.findViewById(R.id.ll_sick);
            rv_LockinInternal = itemView.findViewById(R.id.rv_LockinInternal);
        }



    }
}
